<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_AREA_AD.php' );
require_once( '../system/mdao/Class_M_AREA_AD_SHOP.php');
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_AREA_AD, $login['staff_auth']);

$editflg = 1; //1 編集可能　2 部数だけ編集可能
$is_shop = 2; //1 店舗可能　2　店舗不可 
$isG5 = FALSE ;
if   ($login['staff_auth'] == 'G-5') {
	if( isset($_GET['isshop']) && $_GET['isshop'] != '' ){
		echo "アクセス不可";
		exit;
	}
	$isG5 = TRUE ;
	$editflg = 2;
}

$dao = new Class_M_AREA_AD ;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		$forms["week_cd"]     =  $forms["matrix_cd"]; 
		$daoadshop = new Class_M_AREA_AD_SHOP;
		//$disp_ng = $forms["disp_ng"]?$forms["disp_ng"]:0;

		$wherearr[] = sprintf(" area_ad_shop_id =  '%s' ", $_GET['id'] );
		$dbarray = $daoadshop->search($wherearr );
		//データ整形

		foreach ($dbarray as $k=>$v) {
			$forms["mutirow"] []=array("shop_cd"=> $v["shop_cd"], "shop_name" => $v["shop_name"], "shop_per" => $v["shop_per"], "shop_nums" => $v["shop_nums"]);
		}
		$forms["begin_date"] = Class_PWD::int8_to_date($forms["begin_date"]) ;
		if( isset($_GET['isshop']) && $_GET['isshop'] != '' ){
			if ( ($editflg == 1)  ) {
				$is_shop = 1;
			}
		}
	}else{
		$editflg = 1;
	}

}else{
	$forms = $_POST;
	if( isset($forms['isshop']) && $forms['isshop'] == '1' ){
		if ( ($editflg == 1)  ) {
			$is_shop = 1;
		}
	}
	//echo "**".$is_shop."**";
	if (isset($forms["shop_cd"])) {
		foreach ($forms["shop_cd"] as $k=>$v) {
			$forms["mutirow"] []=array("shop_cd"=> $v, "shop_name" => $forms["shop_name"][$k], "shop_per" => $forms["shop_per"][$k], "shop_nums" => $forms["shop_nums"][$k]);
		}
	} 
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		//if( $forms['id'] == '' ) $err_mes['login_pwd']	= $err->check( $forms['login_pwd'], $chk['login_pwd'] );
		

		if( $err_mes['shop_cd'] == '' ){
			/*
			if( $forms['id'] == '' ){
				$where = sprintf(" shop_cd = '%s'", $forms['shop_cd'] );
				if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['shop_cd'] = '<p class="error">この店舗は重複しています</p>';
				}
			}
			*/
		}
		if( $forms['id'] != '' ){
			$err_mes["mutirow"] = $err->check( $forms["mutirow"],array( "EXIST" ));
			$daoadshop =new Class_M_AREA_AD_SHOP;
			$chkadshop     = $daoadshop->get_checks();
			$index = 0;
			foreach($forms["mutirow"] as $k=>$v) {
				$err_mes["shop_per"][$index]   = $err->check( $v["shop_per"],  $chkadshop["shop_per"] );
				$err_mes["shop_nums"][$index]  = $err->check( $v["shop_nums"], $chkadshop["shop_nums"] );
				$index = $index + 1;
			}
			$where = sprintf(" area_cd = '%s' AND matrix_cd ='%s'  and id <> '%s'", $forms['area_cd'], $forms["week_cd"], $forms['id'] );
			if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['area_cd'] = '<p class="error">この週のエリアは重複しています</p>';
				}
			//echo "*****".$forms['id']."*****";
		}else {
			if( $err_mes['area_cd'] == '' ){
				$where = sprintf(" area_cd = '%s' AND matrix_cd ='%s' ", $forms['area_cd'], $forms["week_cd"] );
				if ( $dao->exits($where) ) {
					$err->clear = false;
					$err_mes['area_cd'] = '<p class="error">この週のエリアは重複しています</p>';
				}
			}
		}
		if( $err->clear ){
			if( $forms['id'] == '' ){
				try {
					$dao->begin_trans();
					$executeflg = TRUE;
					$values = array();
				
					$values["area_name"]         =  $forms["area_name"];
					$values["area_cd"]           =  $forms["area_cd"];
					//class_M_OTHER::do_area($values["area_name"]);
					
					$values["publish_area_name"] =  $forms["publish_area_name"];
					$values["publish_area_cd"]   =  class_M_OTHER::do_publish_area($values["publish_area_name"]);
					
					$values["hand_name"]         =  $forms["hand_name"];
					$values["hand_cd"]           =   class_M_OTHER::do_hand($values["hand_name"]);
					
					$values["media_name"]        =  $forms["media_name"];
					$values["media_cd"]          =  class_M_OTHER::do_media($values["media_name"]);
					
					$values["area_nums"]         =  $forms["area_nums"];
					
					$values["matrix_cd"]         =  $forms["week_cd"]; 
					$values["begin_date"]        =  class_M_OTHER::get_week_flier_date ($forms['week_cd']) ;
					
					$values["memo"]              =  $forms["memo"];
					

					if ($dao->add0($values)) {
						} else {
							$executeflg = FALSE;
						}
					if  ($executeflg) {
						$dao->commit_trans();
						$smarty->assign('message', '登録が完了しました。');
					} else {
						$dao->rollback_trans();
						$smarty->assign('message', '登録が失敗しました。');
					}
				} catch (Exception $e) {
						$dao->rollback_trans();
						$smarty->assign('message', 'ＤＢエラーで失敗しました。');
				}
			}else{
				
				
				/*更新か削除かチェック整形*/
				$dataadd  = array();
				$datadel  = array();
				$dataedit = array();
				try {
					$values = array();
					if ($editflg == 1) {
						$values["area_name"]         =  $forms["area_name"];
						$values["area_cd"]           =  class_M_OTHER::do_area($values["area_name"]);
						
						$values["publish_area_name"] =  $forms["publish_area_name"];
						$values["publish_area_cd"]   =  class_M_OTHER::do_publish_area($values["publish_area_name"]);
						
						$values["hand_name"]         =  $forms["hand_name"];
						$values["hand_cd"]          =  class_M_OTHER::do_hand($values["hand_name"]);
						
						$values["media_name"]        =  $forms["media_name"];
						$values["media_cd"]          =  class_M_OTHER::do_media($values["media_name"]);
						
						$values["area_nums"]         =  $forms["area_nums"];
						
						$values["matrix_cd"]         =  $forms["week_cd"]; 
						$values["begin_date"]        =  class_M_OTHER::get_week_flier_date ($forms['week_cd']) ;
						$values["memo"]              =  $forms["memo"];
						
					}else {
						$values["area_nums"]         =  $forms["area_nums"];
					}
					
					if ($dao->edit( $forms['id'], $values )) {
						if ($dao->updateShopNums( $forms['id'], $values["area_nums"])) {
						$smarty->assign('message', '更新が完了しました。');
						$dao->commit_trans();
						}else {
							$smarty->assign('message', '更新が失敗しました。');
							$dao->rollback_trans();
						}
					} else {
						$smarty->assign('message', '更新が失敗しました。');
						$dao->rollback_trans();
					}
				} catch (Exception $e) {
					$dao->rollback_trans();
					$smarty->assign('message', 'ＤＢエラーで失敗しました。');
				}
				
				if ( $is_shop == 1 ) {
						
					try {	
						$daoadshop->begin_trans();
						
						$executeflg = TRUE;
						//$disp_ng = $forms["disp_ng"]?$forms["disp_ng"]:0;
						$wherearr[] = sprintf(" area_ad_shop_id =  '%s' ", $forms['id'] );
						$dbarray = $daoadshop->search($wherearr );
						//削除.更新のデータ作成
						foreach($dbarray as $k=>$v) {
							$delflg = TRUE;
							$val_shop_nums = "0";
							$val_shop_per  = "0";
							foreach($forms["mutirow"] as $kk=>$vv) {
								if ($v["shop_cd"] == $vv["shop_cd"] ) {
									$delflg = FALSE;
									$val_shop_nums = $vv["shop_nums"];
									$val_shop_per  = $vv["shop_per"];
									break;
								}
							}
							if ($delflg){
								//$datadel[]  = $v["shop_cd"];
								if ($daoadshop->remove($v["id"])) {
								} else {
									$executeflg = FALSE;
								}
								
							} else {
								//$dataedit[] = $v["shop_cd"];
								$values = array();
								$values["shop_nums"] = $val_shop_nums;
								$values["shop_per"]  = $val_shop_per;
								if ($daoadshop->edit( $v["id"] ,$values)) {
								} else {
									$executeflg = FALSE;
								}
							}
						}
						
						//追加のデータ作成
						foreach($forms["mutirow"] as $k=>$v) {
							$taddflg = TRUE;
							foreach($dbarray as $kk=>$vv) {
								if ($v["shop_cd"] == $vv["shop_cd"] ) {
									$taddflg = FALSE;
									break;
								}
							}
							if ($taddflg){
								//$dataadd[] = $v["shop_cd"];
								$values = array();
								$values["area_ad_shop_id"] = $forms['id'];
								$values["shop_cd"]         = $v["shop_cd"] ;
								$values["shop_name"]       = $v["shop_name"] ;
								$values["shop_per"]        = $v["shop_per"] ;
								$values["shop_nums"]       = $v["shop_nums"] ;
								
								if ($daoadshop->add0($values)) {
								} else {
									$executeflg = FALSE;
								}
							}
						}
						/*
						foreach ($datadel as $k=>$v) {
							if ($dao->remove($v)) {
							} else {
								$executeflg = FALSE;
							}
						}
						foreach ($dataedit as $k=>$v) {
							$values = array();
							$values["disp_ng"]          =  $disp_ng;
							if ($dao->edit( $v ,$values)) {
							} else {
								$executeflg = FALSE;
							}
						}
						foreach ($dataadd as $k=>$v) {
							$values = array();
							$values["shop_cd"]          =  $forms["shop_cd"]; 
							$values["shop_cd"]      =  $v;
							$values["shop_shop_cd"] =  $values["shop_cd"]."_".$values["shop_cd"];
							$values["disp_ng"]          =  $disp_ng;
							
							if ($dao->add0($values)) {
							} else {
								$executeflg = FALSE;
							}
						}
						*/
						
						if  ($executeflg) {
							$daoadshop->commit_trans();
							$smarty->assign('message', '更新が完了しました。');
						} else {
							$daoadshop->rollback_trans();
							$smarty->assign('message', '更新が失敗しました。');
						}
					} catch (Exception $e) {
						$daoadshop->rollback_trans();
						$smarty->assign('message', 'ＤＢエラーで失敗しました。');
					}
				}
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

if(isset($forms['id']) && $forms['id'] == '' ) {
	$editflg  = 1;
}

$smarty->assign('editflg', $editflg);
$smarty->assign('isshop', $is_shop);

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$smarty->assign('year4',  Class_PWD::get_years());

$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
$smarty->assign('week', $week );

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);
$smarty->assign('isG5', $isG5 );

$notisg5 = TRUE ;
if   ($login['staff_auth'] == 'G-5') {
	$notisg5 = FALSE ;
}
$smarty->assign('notisg5', $notisg5 );
$area = Class_M_OTHER::get_area_array(PLEASE_SELECT_ARR);
$smarty->assign('area', $area );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mareaad_form.html');
?>